package com.sponsorpay.sdk.android.publisher;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.sponsorpay.sdk.android.SponsorPay;
import com.sponsorpay.sdk.android.credentials.SPCredentials;
import com.sponsorpay.sdk.android.utils.SponsorPayBaseUrlProvider;
import com.sponsorpay.sdk.android.utils.SponsorPayLogger;
import com.sponsorpay.sdk.android.utils.StringUtils;
import com.sponsorpay.sdk.android.utils.UrlBuilder;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class OfferWallActivity extends Activity
{
  private static final String APPID_KEY = "app.id.key";
  public static final String EXTRA_CREDENTIALS_TOKEN_KEY = "EXTRA_CREDENTIALS_TOKEN_KEY";
  public static final String EXTRA_CURRENCY_NAME_KEY = "EXTRA_CURRENCY_NAME_KEY";
  public static final String EXTRA_KEYS_VALUES_MAP_KEY = "EXTRA_KEY_VALUES_MAP";
  public static final String EXTRA_OFFERWALL_TYPE = "EXTRA_OFFERWALL_TEMPLATE_KEY";
  public static final String EXTRA_SHOULD_STAY_OPEN_KEY = "EXTRA_SHOULD_REMAIN_OPEN_KEY";
  public static final String OFFERWALL_TYPE_MOBILE = "OFFERWALL_TYPE_MOBILE";
  public static final String OFFERWALL_TYPE_UNLOCK = "OFFERWALL_TYPE_UNLOCK";
  public static final int RESULT_CODE_NO_STATUS_CODE = -10;
  private static final String SECURITY_TOKEN_KEY = "security.token.key";
  private static final String UID_KEY = "user.id.key";
  private ActivityOfferWebClient mActivityOfferWebClient;
  private SPCredentials mCredentials;
  private String mCurrencyName;
  protected Map<String, String> mCustomKeysValues;
  private AlertDialog mErrorDialog;
  private ProgressDialog mProgressDialog;
  private boolean mShouldStayOpen;
  private OfferWallTemplate mTemplate;
  protected WebView mWebView;

  private String buildUrl()
  {
    this.mCustomKeysValues = this.mTemplate.addAdditionalParameters(this.mCustomKeysValues);
    return UrlBuilder.newBuilder(this.mTemplate.getBaseUrl(), this.mCredentials).setCurrency(this.mCurrencyName).addExtraKeysValues(this.mCustomKeysValues).addScreenMetrics().buildUrl();
  }

  private void deleteCredentialsValues()
  {
    SharedPreferences.Editor localEditor = getPreferences(0).edit();
    localEditor.clear();
    localEditor.commit();
  }

  private void instantiateTemplate()
  {
    if ("OFFERWALL_TYPE_UNLOCK".equals(getIntent().getStringExtra("EXTRA_OFFERWALL_TEMPLATE_KEY")))
    {
      this.mTemplate = new UnlockOfferWallTemplate();
      return;
    }
    this.mTemplate = new MobileOfferWallTemplate();
  }

  private void restoreCredentialsValues()
  {
    SharedPreferences localSharedPreferences = getPreferences(0);
    SponsorPay.start(localSharedPreferences.getString("app.id.key", ""), localSharedPreferences.getString("user.id.key", ""), localSharedPreferences.getString("security.token.key", ""), getApplicationContext());
    this.mCredentials = SponsorPay.getCurrentCredentials();
  }

  private void storeCrendentialsValues()
  {
    SharedPreferences.Editor localEditor = getPreferences(0).edit();
    localEditor.putString("app.id.key", this.mCredentials.getAppId());
    localEditor.putString("user.id.key", this.mCredentials.getUserId());
    localEditor.putString("security.token.key", this.mCredentials.getSecurityToken());
    localEditor.commit();
  }

  protected void fetchPassedExtras()
  {
    String str1 = getIntent().getStringExtra("EXTRA_CREDENTIALS_TOKEN_KEY");
    try
    {
      this.mCredentials = SponsorPay.getCredentials(str1);
      this.mShouldStayOpen = getIntent().getBooleanExtra("EXTRA_SHOULD_REMAIN_OPEN_KEY", this.mTemplate.shouldStayOpenByDefault());
      Serializable localSerializable = getIntent().getSerializableExtra("EXTRA_KEY_VALUES_MAP");
      if ((localSerializable instanceof HashMap))
        this.mCustomKeysValues = ((HashMap)localSerializable);
      String str2 = getIntent().getStringExtra("EXTRA_CURRENCY_NAME_KEY");
      if (StringUtils.notNullNorEmpty(str2))
        this.mCurrencyName = str2;
      this.mTemplate.fetchAdditionalExtras();
      return;
    }
    catch (RuntimeException localRuntimeException)
    {
      while (true)
      {
        restoreCredentialsValues();
        deleteCredentialsValues();
      }
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    getWindow().requestFeature(1);
    this.mProgressDialog = new ProgressDialog(this);
    this.mProgressDialog.setOwnerActivity(this);
    this.mProgressDialog.setIndeterminate(true);
    this.mProgressDialog.setMessage(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.LOADING_OFFERWALL));
    this.mProgressDialog.show();
    instantiateTemplate();
    fetchPassedExtras();
    this.mWebView = new WebView(getApplicationContext());
    this.mWebView.setScrollBarStyle(0);
    setContentView(this.mWebView);
    this.mWebView.getSettings().setJavaScriptEnabled(true);
    this.mWebView.getSettings().setPluginsEnabled(true);
    this.mActivityOfferWebClient = new ActivityOfferWebClient(this, this.mShouldStayOpen)
    {
      public void onReceivedError(WebView paramAnonymousWebView, int paramAnonymousInt, String paramAnonymousString1, String paramAnonymousString2)
      {
        String str = getClass().getSimpleName();
        Object[] arrayOfObject = new Object[3];
        arrayOfObject[0] = Integer.valueOf(paramAnonymousInt);
        arrayOfObject[1] = paramAnonymousString1;
        arrayOfObject[2] = paramAnonymousString2;
        SponsorPayLogger.e(str, String.format("OfferWall WebView triggered an error. Error code: %d, error description: %s. Failing URL: %s", arrayOfObject));
        switch (paramAnonymousInt)
        {
        default:
        case -7:
        case -2:
        }
        for (SponsorPayPublisher.UIStringIdentifier localUIStringIdentifier = SponsorPayPublisher.UIStringIdentifier.ERROR_LOADING_OFFERWALL; ; localUIStringIdentifier = SponsorPayPublisher.UIStringIdentifier.ERROR_LOADING_OFFERWALL_NO_INTERNET_CONNECTION)
        {
          OfferWallActivity.this.showErrorDialog(localUIStringIdentifier);
          return;
        }
      }
    };
    this.mWebView.setWebViewClient(this.mActivityOfferWebClient);
    this.mWebView.setWebChromeClient(new WebChromeClient()
    {
      public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt)
      {
        if ((paramAnonymousInt > 50) && (OfferWallActivity.this.mProgressDialog != null))
        {
          OfferWallActivity.this.mProgressDialog.dismiss();
          OfferWallActivity.this.mProgressDialog = null;
        }
        super.onProgressChanged(paramAnonymousWebView, paramAnonymousInt);
      }
    });
  }

  protected void onPause()
  {
    if (this.mErrorDialog != null)
    {
      this.mErrorDialog.dismiss();
      this.mErrorDialog = null;
    }
    if (this.mProgressDialog != null)
    {
      this.mProgressDialog.dismiss();
      this.mProgressDialog = null;
    }
    storeCrendentialsValues();
    super.onPause();
  }

  protected void onResume()
  {
    super.onResume();
    try
    {
      String str = buildUrl();
      SponsorPayLogger.d(getClass().getSimpleName(), "Offerwall request url: " + str);
      this.mWebView.loadUrl(str);
      return;
    }
    catch (RuntimeException localRuntimeException)
    {
      SponsorPayLogger.e(getClass().getSimpleName(), "An exception occurred when launching the Offer Wall", localRuntimeException);
      this.mActivityOfferWebClient.showDialog(localRuntimeException.getMessage());
    }
  }

  protected void showErrorDialog(SponsorPayPublisher.UIStringIdentifier paramUIStringIdentifier)
  {
    String str = SponsorPayPublisher.getUIString(paramUIStringIdentifier);
    this.mActivityOfferWebClient.showDialog(str);
  }

  public class MobileOfferWallTemplate extends OfferWallActivity.OfferWallTemplate
  {
    private static final String OFW_URL_KEY = "ofw";

    public MobileOfferWallTemplate()
    {
      super();
    }

    public Map<String, String> addAdditionalParameters(Map<String, String> paramMap)
    {
      return paramMap;
    }

    public void fetchAdditionalExtras()
    {
    }

    public String getBaseUrl()
    {
      return SponsorPayBaseUrlProvider.getBaseUrl("ofw");
    }

    public boolean shouldStayOpenByDefault()
    {
      return true;
    }
  }

  public abstract class OfferWallTemplate
  {
    public OfferWallTemplate()
    {
    }

    public abstract Map<String, String> addAdditionalParameters(Map<String, String> paramMap);

    public abstract void fetchAdditionalExtras();

    public abstract String getBaseUrl();

    public abstract boolean shouldStayOpenByDefault();
  }

  public class UnlockOfferWallTemplate extends OfferWallActivity.OfferWallTemplate
  {
    public static final String EXTRA_UNLOCK_ITEM_ID_KEY = "EXTRA_UNLOCK_ITEM_ID_KEY";
    public static final String EXTRA_UNLOCK_ITEM_NAME_KEY = "EXTRA_UNLOCK_ITEM_NAME_KEY";
    public static final String PARAM_UNLOCK_ITEM_ID_KEY = "itemid";
    public static final String PARAM_UNLOCK_ITEM_NAME_KEY = "item_name";
    private static final String UNLOCK_URL_KEY = "unlock";
    private String mUnlockItemId;
    private String mUnlockItemName;

    public UnlockOfferWallTemplate()
    {
      super();
    }

    public Map<String, String> addAdditionalParameters(Map<String, String> paramMap)
    {
      if (paramMap == null)
        paramMap = new HashMap();
      paramMap.put("itemid", this.mUnlockItemId);
      if (StringUtils.notNullNorEmpty(this.mUnlockItemName))
        paramMap.put("item_name", this.mUnlockItemName);
      return paramMap;
    }

    public void fetchAdditionalExtras()
    {
      this.mUnlockItemId = OfferWallActivity.this.getIntent().getStringExtra("EXTRA_UNLOCK_ITEM_ID_KEY");
      this.mUnlockItemName = OfferWallActivity.this.getIntent().getStringExtra("EXTRA_UNLOCK_ITEM_NAME_KEY");
    }

    public String getBaseUrl()
    {
      return SponsorPayBaseUrlProvider.getBaseUrl("unlock");
    }

    public boolean shouldStayOpenByDefault()
    {
      return false;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.sponsorpay.sdk.android.publisher.OfferWallActivity
 * JD-Core Version:    0.6.2
 */